package org.sunnyleaf.votedemo.client.data;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.sunnyleaf.votedemo.client.GreetingServiceAsync;
import org.sunnyleaf.votedemo.client.GreetingServiceAsyncFactory;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Vote implements Serializable{
	
	public static final String OPEN = "接受投票";
	public static final String CLOSED = "投票结束";

	public Vote(){
		
	}
	
	public Vote(String voteName, Date startData, String owner, int numAll) {
		this.voteName = voteName;
		this.startDate = startData;
		this.owner = owner;
		this.totalCount = numAll;
		this.votedCount = 0;

		this.status = "running";
	}

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private Date startDate;

	@Persistent
	private String owner; // 存的是Owner的email

	@Persistent
	private String voteName;

	@Persistent
	private String status;

	@Persistent
	private int totalCount; // 总人数

	@Persistent
	private int votedCount; // 已投人数
	
	@Persistent
	/**
	 * @gwt.typeArgs <java.lang.String>
	 */
	private List<String> voteParticipants;
	
	@NotPersistent
	/**
	 * RPC时用的，不往datastore里存
	 * @gwt.typeArgs <org.sunnyleaf.votedemo.client.data.VoteItem>
	 */
	private List<VoteItem> voteItems;
	
	@NotPersistent
	private boolean isVoted;  // LoginResponse里面用来生成界面
	
	@NotPersistent
	private boolean isParticipated; // 当前登陆用户是否参与
	
	public void setIsParticipated( boolean b ){
		this.isParticipated = b;
	}
	public boolean getIsParticipated(){
		return isParticipated;
	}
	
	/**
	 * @return the key
	 */
	public Long getId() {
		return id;
	}
	
	public void setVoteItem( int index, VoteItem item ){
		voteItems.set(index, item);
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @return the voteName
	 */
	public String getVoteName() {
		return voteName;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @param numAll
	 *            the numAll to set
	 */
	public void setTotalCount(int numAll) {
		this.totalCount = numAll;
	}

	/**
	 * @return the ownerId
	 */
	public String getOwner() {
		return owner;
	}

	/**
	 * @param ownerId
	 *            the ownerId to set
	 */
	public void setOwner(String owner) {
		this.owner = owner;
	}

	/**
	 * @return the votedCount
	 */
	public int getVotedCount() {
		return votedCount;
	}

	/**
	 * @param votedCount
	 *            the votedCount to set
	 */
	public void setVotedCount(int votedCount) {
		this.votedCount = votedCount;
	}

	/**
	 * @return the totalCount
	 */
	public int getTotalCount() {
		return totalCount;
	}

	/**
	 * @param startDate
	 *            the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @param voteName
	 *            the voteName to set
	 */
	public void setVoteName(String voteName) {
		this.voteName = voteName;
	}

	/**
	 * @return the voteItems
	 */
	public List<VoteItem> getVoteItems() {
		return voteItems;
	}

	/**
	 * @param voteItems the voteItems to set
	 */
	public void setVoteItems(List<VoteItem> voteItems) {
		this.voteItems = voteItems;
	}

	public void updateVoteItems(){
		GreetingServiceAsync gsa = GreetingServiceAsyncFactory.getServiceInstance();
		gsa.getVoteItems(this.id, new AsyncCallback<List<VoteItem>>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getCause().getMessage());
			}

			@Override
			public void onSuccess(List<VoteItem> result) {
				setVoteItems(result);
			}
			
		});
	}
	
	public boolean isVoted() {
		return isVoted;
	}

	public void setVoted(boolean isVoted) {
		this.isVoted = isVoted;
	}

	public void setVoteParticipants(List<String> voteParticipants) {
		this.voteParticipants = voteParticipants;
	}

	public List<String> getVoteParticipants() {
		return voteParticipants;
	}

}
